<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class tin_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library("unicode");
	}
	public function getNews()
	{
		$query = $this->db->query("SELECT ten, tenUrl, hinh, tomtat FROM tin WHERE anhien = 1 ORDER BY thutu");
		return $query->result();
	}
	public function getNewsFromTenUrl($tenUrl)
	{
		$query = $this->db->query("SELECT ten, hinh, tomtat, noidung FROM tin WHERE anhien = 1 AND tenUrl = ?",array($tenUrl));
		return $query->result();
	}
	public function getNewsFromTenLT($tenLT)
	{
		$query = $this->db->query("SELECT tin.ten, tin.tenUrl, tin.hinh, tin.tomtat FROM tin, loaitin WHERE loaitin.tenUrl = ? AND tin.idLT = loaitin.id AND loaitin.anhien = 1 AND tin.anhien = 1 ORDER BY tin.thutu, loaitin.thutu",array($tenLT));
		return $query->result();
	}
	public function image($id,$filename)
	{
		$query = $this->db->query("SELECT hinh FROM tin WHERE id = ?",array($id));
		$link = './upload/tin/'.$query->row()->hinh;
		if(is_file($link))
		{
			unlink($link);
		}
		$this->db->query("UPDATE tin SET hinh = ? WHERE id = ?", array($filename, $id));
		$chk = $this->db->query("SELECT id FROM tin WHERE hinh = ? AND id = ?", array($filename, $id));
		if($chk->num_rows() > 0) return TRUE;
		return FALSE;
	}
	public function select()
	{
		$query = $this->db->query("SELECT tin.id, loaitin.ten AS tenLT, tin.ten, tin.thutu, tin.anhien FROM tin, loaitin WHERE tin.idLT = loaitin.id ORDER BY thutu");
		return $query->result();
	}
	public function edit($id)
	{
		$query = $this->db->query("SELECT * FROM tin WHERE id = ?",array($id));
		return $query->result();
	}
	public function insert($idLT, $ten, $tomtat, $noidung, $thutu)
	{
		$tenUrl = $this->unicode->change($ten);
		$tomtat = $this->unicode->unicode2database($tomtat);
		$noidung = $this->unicode->unicode2database($noidung);
		if(is_numeric($thutu)) settype($thutu,"int"); else $thutu = 0;
		if(is_numeric($idLT)) settype($idLT,"int"); else $idLT = 0;
		if($idLT>0)
		{
			$this->db->query("INSERT INTO tin VALUES (NULL, ?, ?, ?, NULL, ?, ?, ?, 1)", array($idLT, $ten, $tenUrl, $tomtat, $noidung, $thutu));
			return $this->db->insert_id();
		}
		else return 0;
	}
	public function update($id, $idLT, $ten, $tomtat, $noidung, $thutu, $anhien)
	{
		$tenUrl = $this->unicode->change($ten);
		$tomtat = $this->unicode->unicode2database($tomtat);
		$noidung = $this->unicode->unicode2database($noidung);
		if(is_numeric($idLT)) settype($idLT,"int"); else $idLT = 0;
		if($idLT>0)
		{
			$this->db->query("UPDATE tin SET idLT = ?, ten = ?, tenUrl = ?, tomtat = ?, noidung = ?, thutu = ?, anhien = ? WHERE id = ?", array($idLT, $ten, $tenUrl, $tomtat, $noidung, $thutu, $anhien, $id));
			$chk = $this->db->query("SELECT id FROM tin WHERE idLT = ? AND ten = ? AND tenUrl = ? AND tomtat = ? AND noidung = ? AND thutu = ? AND anhien = ? AND id = ?", array($idLT, $ten, $tenUrl, $tomtat, $noidung, $thutu, $anhien, $id));
			if($chk->num_rows() > 0) return TRUE;
		}
		return FALSE;
	}
	public function delete($id)
	{
		$query = $this->db->query("SELECT hinh FROM tin WHERE id = ?",array($id));
		$link = './upload/tin/'.$query->row()->hinh;
		if(is_file($link)) unlink($link);
		$this->db->query("DELETE FROM tin WHERE id = ?",array($id));
		$query = $this->db->query("SELECT id FROM tin WHERE id = ?",array($id));
		if($query->num_rows()>0) return FALSE; return TRUE;
	}
	public function deleteImage($id)
	{
		$query = $this->db->query("SELECT hinh FROM tin WHERE id = ?",array($id));
		$link = './upload/tin/'.$query->row()->hinh;
		if(is_file($link)) unlink($link);
		$this->db->query("UPDATE tin SET hinh = NULL WHERE id = ?",array($id));
	}
}